package top.hkyzf.study.leetcode.array;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * description: 
 * @author zhufeng
 * @date 2023/4/13 9:01
 * @version V1.0
 */
public class 出现最频繁的偶数元素_2404 {

    @Test
    public void testMostFrequentEven () {
        int[] nums = {2,10000,10000,10000,2};
        int i = mostFrequentEven(nums);
        System.out.println(i);
    }

    public int mostFrequentEven(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        // 值，次数
        int[] maxArr = {-1, -1};
        for (int num : nums) {
            // 偶数计数
            if (num % 2 == 0) {
                int max = map.getOrDefault(num, 0) + 1;
                map.put(num, max);
                // map.merge(num, 1, Integer::sum);
                if (max > maxArr[1]) {
                    maxArr[0] = num;
                    maxArr[1] = max;
                }
                if (max == maxArr[1] && num < maxArr[0]) {
                    maxArr[0] = num;
                }
            }
        }
        return maxArr[0];
    }
}
